package com.abbas.smartCity.ui;

import android.graphics.Color;

import com.abbas.smartCity.R;
import com.abbas.smartCity.base.BaseFragment;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 数据分析模块
 * 数据难以筛选分类
 */
public class DaFragment extends BaseFragment {

    /**
     * {@link com.github.mikephil.charting.charts}
     */
    private LineChart lineChart;//线性
    private BarChart barChart;//条形
    private HorizontalBarChart horizontalBarChart;//横向条形
    private CombinedChart combinedChart;//结合
    private PieChart pieChart;//饼图
    private RadarChart radarChart;//雷达
    //气泡...

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_da;
    }

    @Override
    protected void initView() {
        lineChart = (LineChart) rootView.findViewById(R.id.lineChart);
        barChart = (BarChart) rootView.findViewById(R.id.barChart);
        horizontalBarChart = (HorizontalBarChart) rootView.findViewById(R.id.horizontalBarChart);
        combinedChart = (CombinedChart) rootView.findViewById(R.id.combinedChart);
        pieChart = (PieChart) rootView.findViewById(R.id.pieChart);
        radarChart = (RadarChart) rootView.findViewById(R.id.radarChart);
    }

    @Override
    protected void initData() {
        //随机类
        final Random random = new Random();

        final ArrayList<Entry> lineEntries = new ArrayList<>();
        final List<String> lineTitles = Arrays.asList("新闻1", "新闻2", "新闻3", "新闻4", "新闻5", "新闻6", "新闻7", "新闻8");
        for (int i = 0; i < 8; i++) {
            lineEntries.add(new Entry(i,random.nextInt(100)));
        }
        final LineDataSet lineDataSet = new LineDataSet(lineEntries,"新闻点赞数量");
        lineDataSet.setColor(Color.RED);
        final LineData lineData = new LineData(lineDataSet);
        lineChart.getDescription().setEnabled(false);//取消描述
        lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴在底部
        lineChart.getAxisRight().setEnabled(false);//取消右边Y轴
        lineChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(lineTitles));//设置X轴数据
        lineChart.setData(lineData);//设置数据

        final ArrayList<BarEntry> barEntries = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            barEntries.add(new BarEntry(i,random.nextInt(100)));
        }
        final BarDataSet barDataSet = new BarDataSet(barEntries,"新闻评论数量");
        barDataSet.setColor(Color.YELLOW);
        final BarData barData = new BarData(barDataSet);
        barChart.getDescription().setEnabled(false);
        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        barChart.getAxisRight().setEnabled(false);
        barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(lineTitles));
        barChart.setData(barData);

        final ArrayList<BarEntry> horiBarEntries = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            horiBarEntries.add(new BarEntry(i,random.nextInt(100)));
        }
        final BarDataSet horiBarDataSet = new BarDataSet(horiBarEntries,"新闻评论数量");
        final BarData horiBarData = new BarData(horiBarDataSet);
        horizontalBarChart.getDescription().setEnabled(false);
        horizontalBarChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        horizontalBarChart.getAxisLeft().setEnabled(false);
        horizontalBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(lineTitles));
        horizontalBarChart.setData(horiBarData);

        combinedChart.setDrawOrder(new CombinedChart.DrawOrder[]{
                CombinedChart.DrawOrder.BAR,
                CombinedChart.DrawOrder.LINE
        });
        final CombinedData combinedData = new CombinedData();
        combinedData.setData(lineData);
        combinedData.setData(barData);
        combinedChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(lineTitles));
        combinedChart.setData(combinedData);

        final ArrayList<PieEntry> pieEntries = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            pieEntries.add(new PieEntry(25,"测试"));
        }
        final PieDataSet pieDataSet = new PieDataSet(pieEntries,"新闻前四条点赞比率");
        pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//设置X Y 的数据坐标在图表外面
        pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        barDataSet.setValueTextColor(Color.BLACK);
        barDataSet.setValueTextSize(16);
        List<Integer> colors = Arrays.asList(getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorPrimaryDark),getResources().getColor(R.color.gray_800));
        pieDataSet.setColors(colors);
        final PieData pieData = new PieData(pieDataSet);
        pieData.setValueFormatter(new PercentFormatter(pieChart));//设置显示数据为百分比模式
        pieChart.setEntryLabelColor(Color.parseColor("#000000"));
        pieChart.setUsePercentValues(true);//启用百分比数据模式
        pieChart.setData(pieData);

        final ArrayList<RadarEntry> radarEntries = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            radarEntries.add(new RadarEntry(random.nextInt(100)));
        }
        final RadarDataSet radarDataSet = new RadarDataSet(radarEntries,"????");
        final RadarData radarData = new RadarData(radarDataSet);
        radarChart.setData(radarData);
    }

    @Override
    protected void initEvent() {

    }

}